﻿/**
 * date: 2011-5-20
 * author : Eason
 * description : 将影片剪辑转变为位图影片的创建者类.
 * 可调用静态方法create进行创建.
 **/
package com.pickgliss.bitmapEngin.creater
{
	import com.pickgliss.bitmapEngin.cache.CacheFrame;
	import com.pickgliss.bitmapEngin.mode.BitmapMovieClipData;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;

	public class BitmapMovieClipDataCreater
	{
		private var _mc : MovieClip;
		private var _id : String;
		private var _async : Boolean;
		
		private var _data : BitmapMovieClipData;
		
		private var _currentFrame : int;
		private static const STEP_COUNT : int = 8;
		
		public function BitmapMovieClipDataCreater(mc : MovieClip, id : String, async : Boolean)
		{
			_mc = mc;
			_id = id;
			_async = async;
			
			_currentFrame = 1;
		}
		private function create():BitmapMovieClipData
		{
			_data = new BitmapMovieClipData();
			_data.id = _id;
			_data.frames = new Vector.<CacheFrame>();
			_data.labels = _mc.currentLabels;
			if(!_async){
				createFrames(_mc.totalFrames);
				_data.inited = true;
			}else{
				startCreateByAsync();
			}
			
			return _data;
		}
		
		/**
		 * 开始按异步的方式进行创建. 
		 * 
		 */		
		private function startCreateByAsync():void
		{
			createStep();
		}
		
		/**
		 * 创建步骤. 
		 * 
		 */		
		private var _timeId : int;
		private function createStep():void
		{
			var count : int = _mc.totalFrames - _currentFrame + 1;
			if(count > STEP_COUNT){
				count = STEP_COUNT
			}
			createFrames(count);
			_currentFrame += count;
			if(_timeId > 0)clearTimeout(_timeId);_timeId = 0;
			if(_currentFrame < _mc.totalFrames){
				_timeId = setTimeout(createStep, 10);
			}else{
				_data.inited = true;
			}
		}
		private function createFrames(frame : int):void
		{
			var rect : Rectangle ;
			var matrix : Matrix = new Matrix();
			var registerX : int ;
			var registerY : int ;
			var endFrame : int = _currentFrame + frame
			for ( var i : int = _currentFrame ; i < endFrame; ++i )
			{
				_mc.gotoAndStop (i);
				rect = _mc.getBounds(_mc);
				matrix.identity();
				
				
				registerX = int(-rect.x);
				registerY = int(-rect.y);
				
				var k:int =  _mc.numChildren;
				for (var j:int = 0 ; j<k; ++j)
				{
					var child:MovieClip =  _mc.getChildAt(j) as MovieClip;
					if (child)
					{
						if ( child.currentFrame != child.totalFrames)
						{
							child.gotoAndStop(child.currentFrame+1);
						}else{
							child.gotoAndStop(1);
						}
					}
				}
				matrix.translate(registerX, registerY);
				
				var w : int = _mc.width;
				var h : int = _mc.height
				w = w > 0? w : 1;
				h = h > 0? h : 1;
				
				var bitmapData:BitmapData = new BitmapData(w, h, true, 0x00000000);
				bitmapData.draw(_mc,matrix);
				_data.frames.push(new CacheFrame(bitmapData,registerX,registerY));
				bitmapData = null;
			}
		}
		
		
		/**
		 * 根据影片剪辑创建位图影片所用的数据.
		 * @param mc
		 * @param id
		 * @param async
		 * @return 
		 * 
		 */		
		public static function create(mc : MovieClip, id : String, async : Boolean):BitmapMovieClipData
		{
			var creater : BitmapMovieClipDataCreater = new BitmapMovieClipDataCreater(mc,id,async);
			return creater.create();
		}
	}
}